<template>
	<view class="content">
		<button class="size" @tap="getCurrentPosition">获取当前设备位置信息</button>
		<view v-for="(site,index) in location.address" :key="index">{{site}}</view>
		<view v-for="(site,index) in location.coords" :key="index">{{site}}</view>
		<button class="size" @tap="watchPosition">监听设备位置变化信息 </button>
		<view v-for="(site,index) in watchLocation.address" :key="index">{{site}}</view>
		<view v-for="(site,index) in watchLocation.coords" :key="index">{{site}}</view>
		<button class="size" @tap="clearWatch">关闭监听设备位置信息</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				location: {},
				watchLocation: {},
				wid:null
			}
		},
		methods: {
			getCurrentPosition() {
				// 这个方法有作用域问题
				let that = this;
				plus.geolocation.getCurrentPosition(function(p) {
					console.log("p=>", p)
					that.location = p;
				}, function(e) {
					console.log('Geolocation error: ' + e.message);
				});
			},
			watchPosition() {
				let that = this;
				that.wid =  plus.geolocation.watchPosition(function(p) {
					that.watchLocation = p;
				}, function(e) {
					console.log('Geolocation error: ' + e.message);
				});
			},
			clearWatch() {
				plus.geolocation.clearWatch(this.wid);
				this.wid = null;
			},
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}

	.size {
		margin: 30rpx 0;
	}
</style>
